/**
 * Created by qian.zhou on 2017/2/23.
 */

var React = require('react');

var Icon = require('../components/Icon');
var ajax= require('../tools/ajax');

module.exports = React.createClass({
    getInitialState:function(){
        return {
            cate_id:"",//
            proInfo:"",//所在类型的商品信息
            flag:false
        }
    },
    componentDidMount:function(){
        var cate_id = this.props.cate_id;
        this.getGoodsInfoByCateid(cate_id);
    },
    action: function (e){
        var action = e.currentTarget.getAttribute("action");
        var id = e.currentTarget.getAttribute("data");
        var price = e.currentTarget.getAttribute("data-price");
        localStorage.setItem("back","#fenlei");
        localStorage.setItem("goods_id",id);
        localStorage.setItem("price",price);
        location.href = action;
    },
    /**获取二级类目下的商品*/
    getGoodsInfoByCateid(cate_id){
        var p =this;
        var limit = 0;
        var countryid = this.props.countryid;
        ajax("POST","getGoodsInfoByCateid",{cate_id:cate_id,countryid:countryid,limit:limit},function(dt) {
            if (dt && dt.success) {
                var obj = dt.data;
                p.setState({proInfo:obj});
                if(obj.length < 6){
                    p.setState({flag:false});
                }else{
                    p.setState({flag:true});
                }
            }
        })
    },
    more(e){
        var countryid = e.currentTarget.getAttribute("data-countryid");
        console.log(countryid);
        localStorage.cateId = this.props.cate_id;
        localStorage.setItem("countryid",countryid);
        localStorage.searchPro ="";
        location.href = "#fenleiPro";
    },
    render:function(){
        var p = this;
        var list = this.state.proInfo;
        if(list){
            var actlist = list.map(function(obj,index){
                if(index!=5) {
                    return (
                        <div key={index} className="item" onClick={p.action} action="#goodDetail" data={obj.id}
                             data-price={obj.ProPrice}>
                            <div className="pro-img">
                                <img src={obj.goods_img.split(",")[0]}/>
                            </div>
                            <div className="pro-msg">
                                <p className="item_title">{obj.goods_name}</p>
                            </div>
                            <div className="pro-add">
                                <div className="pro_price">
                                    ￥{obj.ProPrice}
                                </div>
                                <Icon icon="#icon-gouwuche-tianjia" style={{color:"#999",fontSize:"1rem"}}></Icon>
                            </div>
                        </div>
                    )
                }
            });
        }
        return (
            <div className="gj-pro">
                {actlist}
                <div className="item more" onClick={p.more} style={{display:p.state.flag?"block":"none"}} data-countryid={this.props.countryid}>
                    <Icon icon="#icon-fanhui" style={{fontSize:"3rem"}}/>
                </div>
            </div>
        )
    }
});